package st.business.tepth.business.goodsorder;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;

import java.util.ArrayList;
import java.util.List;

import st.business.tepth.R;
import st.business.tepth.base.OnItemChooseListener;
import st.business.tepth.business.bean.PickGoods;
import st.business.tepth.business.goodsreceive.GoodsReceiveConvert;
import st.business.tepth.config.ItemFields;
import st.shadow.library.ui.recycler.BaseDecoration;
import st.shadow.library.ui.recycler.MultipleItemEntity;

/**
 * @author zhangyu
 * @date 2019/2/23
 */
public class DialogReturnGoods extends Dialog {
    public static final int FIELD_SELECT_COUNT = 1001;
    private List<MultipleItemEntity> mItemEntities;

    public DialogReturnGoods(final Context context, final List<PickGoods> list,
                             final OnItemChooseListener<String> listener) {
        super(context);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        View view = LayoutInflater.from(context).
                inflate(R.layout.dialog_return_goods, null);
        RecyclerView recyclerView = view.findViewById(R.id.recycler_view_goods);
        view.findViewById(R.id.iv_close).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });

        recyclerView.setLayoutManager(new LinearLayoutManager(context));
        recyclerView.addItemDecoration(BaseDecoration.
                create(Color.parseColor("#f0f0f0"), 1));
        mItemEntities = wrapGoodsBean(list);
        final BaseQuickAdapter adapter = new BaseQuickAdapter<MultipleItemEntity, BaseViewHolder>
                (R.layout.item_return_goods, mItemEntities) {
            @Override
            protected void convert(final BaseViewHolder helper, final MultipleItemEntity item) {
                final PickGoods goodsBean = item.getField(ItemFields.ITEM_OBJ);
                helper.setText(R.id.tv_name, goodsBean.getMaterialName());
                helper.setText(R.id.tv_version, goodsBean.getModelNo());
                helper.setText(R.id.v_count, goodsBean.getValue() + "");
                helper.setText(R.id.tv_count, item.getField(FIELD_SELECT_COUNT) + "");
                helper.getView(R.id.iv_minus).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        int count = item.getField(FIELD_SELECT_COUNT);
                        if (count > 1) {
                            //数量大于1就可以减
                            item.setField(FIELD_SELECT_COUNT, count - 1);
                            helper.setText(R.id.tv_count,
                                    item.getField(FIELD_SELECT_COUNT) + "");
                        }
                    }
                });
                helper.getView(R.id.iv_add).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        int count = item.getField(FIELD_SELECT_COUNT);
                        if (count < goodsBean.getValue()) {
                            //小于最大值就可以加
                            item.setField(FIELD_SELECT_COUNT, count + 1);
                            helper.setText(R.id.tv_count,
                                    item.getField(FIELD_SELECT_COUNT) + "");
                        }
                    }
                });
            }
        };
        view.findViewById(R.id.tv_confirm).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (listener != null) {
                    listener.onChoose(new GoodsReceiveConvert()
                            .convertToIDBeanString(mItemEntities));
                }
                dismiss();
            }
        });
        adapter.addHeaderView(LayoutInflater.
                from(context).inflate(R.layout.item_head_return_goods,
                null));
        recyclerView.setAdapter(adapter);
        setContentView(view);
    }

    private List<MultipleItemEntity> wrapGoodsBean(List<PickGoods> list) {
        List<MultipleItemEntity> entities = new ArrayList<>();
        for (PickGoods goodsBean : list) {
            entities.add(MultipleItemEntity.builder()
                    .setField(ItemFields.ITEM_OBJ, goodsBean)
                    .setField(FIELD_SELECT_COUNT, 1)
                    .build());
        }
        return entities;
    }

}
